package com.xii.service;


import com.alibaba.dubbo.config.annotation.Service;
import com.xii.constant.MessageConstant;
import com.xii.constant.RedisConstant;
import com.xii.entity.Result;
import com.xii.mapper.MemberMapper;
import com.xii.mapper.OrderMapper;
import com.xii.mapper.OrderSettingMapper;
import com.xii.pojo.Member;
import com.xii.pojo.Order;
import com.xii.pojo.OrderSetting;
import com.xii.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import redis.clients.jedis.JedisPool;

import java.util.Date;
import java.util.Map;
@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private JedisPool jedisPool;

    @Autowired
    private OrderSettingMapper orderSettingMapper;
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private MemberMapper memberMapper;

    @Override
    public Result submit(Map map) {
        try {
            //1、判断用户输入验证码是否正确
            String telephone = (String) map.get("telephone");
            String validateCode = (String) map.get("validateCode");
            String redisCode = jedisPool.getResource().get(RedisConstant.ORDER_CODE + "_" + telephone);
            if(validateCode == null || !validateCode.equals(redisCode)){
                return new Result(false, MessageConstant.VALIDATECODE_ERROR);
            }
            //2、判断当前日期是否有预约名额
            String orderDdate = (String) map.get("orderDate");
            OrderSetting orderSetting = orderSettingMapper.getOrderSettingByDate(DateUtils.parseString2Date(orderDdate));
            if(orderSetting == null){
                return new Result(false, MessageConstant.SELECTED_DATE_CANNOT_ORDER);
            }
            //3、查看当前日期是否已经约满
            int number = orderSetting.getNumber();
            int reservations = orderSetting.getReservations();
            if(number <= reservations){
                return new Result(false, MessageConstant.ORDER_FULL);
            }
            //4、查看是否重复预约(同一个身份证号，同一天，同一个套餐)
                //身份证号查找用户
            Member member = memberMapper.getMemberByIdCard((String) map.get("idCard"));
            if(member != null){
                //用户存在的话
                Order order = orderMapper.getOrderByMemberDateSetmeal(member.getId(),DateUtils.parseString2Date((String) map.get("orderDate")),(String)map.get("setmealId"));
                //order存在为已经预约
                if(order != null){
                    return new Result(false, MessageConstant.HAS_ORDERED);
                }

            }else{
            //5、查看是否为会员 不是则进行注册
                member = new Member();
                member.setName((String) map.get("name"));
                member.setSex((String) map.get("sex"));
                member.setPhoneNumber(telephone);
                member.setRegTime(new Date());
                member.setIdCard((String) map.get("idCard"));
                memberMapper.addMember(member);
            }
            //6、预约成功 更新当前预约人数  更新ordersetting表   //TODO
            orderSetting.setReservations(orderSetting.getReservations()+1);
            orderSettingMapper.updateOrderSettingByDate(orderSetting);
            //更新order表
            Order order = new Order(member.getId(),DateUtils.parseString2Date(orderDdate), Order.ORDERTYPE_WEIXIN, Order.ORDERSTATUS_NO, Integer.parseInt((String) map.get("setmealId")));
            //添加预约信息
            orderMapper.addOrder(order);
            return new Result(true, MessageConstant.ORDER_SUCCESS,order.getId());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ORDER_FAIL);
        }

    }

    @Override
    public Map findById(Integer id)  {
        Map map = null;
        try {
            map = orderMapper.findDetailsById(id);
            System.out.println("MAP:"+map);
            if(map!=null){
                Date orderDate = (Date)map.get("orderDate");
                map.put("orderDate",DateUtils.parseDate2String(orderDate));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;

    }
}
